package cloud.keveri.response4j;

import cloud.keveri.base.DefaultResponseStatus;
import cloud.keveri.base.result.Response;
import cloud.keveri.base.result.ResponseStatus;
import cloud.keveri.response4j.configure.Response4jProperties;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 作者：沈自在
 **/
@Slf4j
public class DefaultResponseMaker implements ResponseMaker, ResponseStatusProvider {

    /**
     *
     */
    private final ResponseManager responseManager;

    private final Response4jProperties prop;

    public DefaultResponseMaker(ResponseManager responseManager, Response4jProperties prop) {
        this.responseManager = responseManager;
        this.prop = prop;
    }

    /**
     *
     */
    @Override
    public Response newEmptyInstance() {
        return responseManager.buildEmptyResponse();
    }

    /**
     *
     */
    @Override
    public Response newInstance(ResponseStatus status) {
        Response response = this.newEmptyInstance();
        response.setStatus(status);
        return response;
    }

    /**
     *
     */
    @Override
    public Response newSuccessInstance() {
        Response emptyInstance = this.newEmptyInstance();
        emptyInstance.setStatus(defaultSuccess());
        return emptyInstance;
    }

    /**
     *
     */
    @Override
    public Response newSuccessInstance(Object data) {
        Response instance = this.newEmptyInstance();
        instance.setStatus(defaultSuccess());
        instance.setPayload(data);
        return instance;
    }

    /**
     *
     */
    @Override
    public Response newFailInstance() {
        Response emptyInstance = this.newEmptyInstance();
        emptyInstance.setStatus(defaultError());
        return emptyInstance;
    }

    /**
     *
     */
    @Override
    public ResponseStatus defaultSuccess() {
        return DefaultResponseStatus.builder()
                .message(prop.getDefaultSuccessMessage())
                .code(prop.getDefaultSuccessCode())
                .build();
    }

    /**
     *
     */
    @Override
    public ResponseStatus defaultError() {
        return DefaultResponseStatus.builder()
                .message(prop.getDefaultErrorMessage())
                .code(prop.getDefaultErrorCode())
                .build();
    }

    /**
     *
     */
    @Override
    public ResponseStatus newInstance(Integer code, String message) {
        return DefaultResponseStatus.builder()
                .code(code)
                .message(message)
                .build();
    }
}
